/*
 * @Author: szx
 * @Date: 2022-01-21 14:25:10
 * @LastEditTime: 2022-01-21 14:38:03
 * @Description:
 * @FilePath: \leetcode\700-799\718\718.js
 */
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var findLength = function (nums1, nums2) {
    let res = 0;
    const dp = new Array(nums1.length + 1).fill(0).map(() => new Array(nums2.length + 1).fill(0));
    for (let i = 1; i <= nums1.length; i++) {
        for (let j = 1; j <= nums2.length; j++) {
            if (nums1[i - 1] == nums2[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }
            if (dp[i][j] > res) res = dp[i][j];
        }
    }
    return res;
};

// 滚动数组
var findLength = function (nums1, nums2) {
    let res = 0;
    const dp = new Array(nums2.length + 1).fill(0);
    for (let i = 1; i <= nums1.length; i++) {
        for (let j = 1; j <= nums2.length; j++) {
            if (nums1[i - 1] == nums2[j - 1]) {
                dp[j] = dp[j - 1] + 1;
            }
            if (dp[j] > res) res = dp[j];
        }
    }
    return res;
};
